package com.example.elderlycare.controller;

import com.example.elderlycare.controller.entity.EmployeeRequestBody;
import com.example.elderlycare.dao.entity.Employee;
import com.example.elderlycare.service.EmployeeService;
import com.example.elderlycare.service.entity.EmployeeDragListResponseBody;
import com.example.elderlycare.service.entity.EmployeeListResponseBody;
import com.example.elderlycare.utils.Result;
import com.example.elderlycare.utils.ResultEnum;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/employee")
public class EmployeeController {
    @Resource
    EmployeeService employeeService;

    /**
     * queryEmployeeList 根据条件获取列表
     *
     * @param pageSize 页面大小
     * @param current  当前页面
     * @return EmployeeListResponseBody
     */
    @GetMapping
    public Result<EmployeeListResponseBody> queryEmployeeList(
            @RequestParam(value = "name", required = false) String name,
            @RequestParam(value = "number", required = false) String number,
            @RequestParam(value = "sex", required = false) Integer sex,
            @RequestParam(value = "status", required = false) Integer status,
            @RequestParam(value = "position", required = false) Integer position,
            @RequestParam(value = "nurse", required = false) Integer nurse,
            @RequestParam(value = "ids", required = false) List<Integer> ids,
            @RequestParam(defaultValue = "10", value = "pageSize") int pageSize,
            @RequestParam(defaultValue = "1", value = "current") int current
    ) {
        Map<String, Object> params = new HashMap<>();
        params.put("name", name != null ? "%" + name + "%" : null);
        params.put("number", number != null ? "%" + number + "%" : null);
        params.put("sex", sex);
        params.put("status", status);
        params.put("position", position);
        params.put("nurse", nurse);
        params.put("ids", ids);
        params.put("limit", pageSize);
        params.put("offset", (current - 1) * pageSize);
        EmployeeListResponseBody responseBody = employeeService.queryEmployeeList(params);
        if (responseBody == null) {
            Result.error(ResultEnum.DEFAULT_ERROR);
        }
        return Result.success(responseBody);
    }

    @GetMapping("/drag")
    public Result<List<EmployeeDragListResponseBody>> queryEmployeeDragList() {
        List<Employee> employeeList = employeeService.list();
        List<EmployeeDragListResponseBody> items = employeeList.stream().
                map(item -> EmployeeDragListResponseBody.builder().
                        id(item.getId()).
                        name(item.getName()).
                        build()).collect(Collectors.toList());
        return Result.success(items);
    }

    @PostMapping
    public Result<?> addEmployee(@Valid @RequestBody EmployeeRequestBody.AddEmployeeRequestBody params) {
        Employee employee = Employee.builder().
                name(params.getName()).
                number(params.getNumber()).
                phone(params.getPhone()).
                sex(params.getSex()).
                entryTime(params.getEntryTime()).
                departmentId(params.getDepartmentId()).
                fatherId(params.getFatherId()).
                cardTypeId(params.getCardTypeId()).
                nurseId(params.getNurseId()).
                userStatusId(params.getUserStatusId()).
                positionId(params.getPositionId()).
                educationId(params.getEducationId()).
                build();
        boolean success = employeeService.save(employee);
        if (!success) {
            return Result.error(ResultEnum.DEFAULT_ERROR);
        }
        return Result.success(null);
    }

    @PutMapping
    public Result<?> updateEmployee(@Valid @RequestBody EmployeeRequestBody.UpdateEmployeeRequestBody params) {
        Employee employee = Employee.builder().
                id(params.getId()).
                name(params.getName()).
                number(params.getNumber()).
                phone(params.getPhone()).
                sex(params.getSex()).
                entryTime(params.getEntryTime()).
                departmentId(params.getDepartmentId()).
                fatherId(params.getFatherId()).
                cardTypeId(params.getCardTypeId()).
                nurseId(params.getNurseId()).
                userStatusId(params.getUserStatusId()).
                positionId(params.getPositionId()).
                educationId(params.getEducationId()).
                build();
        boolean success = employeeService.updateById(employee);
        if (!success) {
            return Result.error(ResultEnum.DEFAULT_ERROR);
        }
        return Result.success(null);
    }

    @DeleteMapping
    public Result<?> deleteEmployee(@Valid @RequestParam("id") Integer id) {
        boolean success = employeeService.removeById(id);
        if (!success) {
            return Result.error(ResultEnum.DEFAULT_ERROR);
        }
        return Result.success(null);
    }
}
